//
//  ZZTabBarController.m
//  NetesaeLottery
//
//  Created by 周英培 on 16/8/1.
//  Copyright © 2016年 周英培. All rights reserved.
//

#import "ZZTabBarController.h"
#import "ZZBottomView.h"
@interface ZZTabBarController ()<ZZBottomViewDelegate>

@end

@implementation ZZTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //MARK:加载子控制器
    [self setUpChildViewControllers];
    
    //MARK:加载底部视图
    [self setUpBottomView];

}

/// 加载底部视图
- (void)setUpBottomView {

    ZZBottomView *bottomView = [[ZZBottomView alloc] initWithFrame:self.tabBar.bounds];
    
    bottomView.backgroundColor = ZZRandomColor;
    
    bottomView.delegate = self;
    
    [self.tabBar addSubview:bottomView];
    
    // MARK: - 根据当前标签控制器内子控制器的数量 去 添加按钮 并且 给它按钮的图片
    [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UINavigationController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSString *imgName = [NSString stringWithFormat:@"TabBar%@",@(idx + 1)];
        
        NSString *selImgName = [NSString stringWithFormat:@"TabBar%@Sel",@(idx + 1)];
        
        [bottomView creatBtnWithImgName:imgName selImgName:selImgName];
        
    }];

}

- (void)bottomView:(ZZBottomView *)bottomView didSelectIndex:(NSInteger)index {

    self.selectedIndex = index;

}

/// 加载子控制器
- (void)setUpChildViewControllers {

    UINavigationController *hallNav = [self navigationControllerWithSbName:@"Hall"];
    
    UINavigationController *arenaNav = [self navigationControllerWithSbName:@"Arena"];
    
    UINavigationController *discoverNav = [self navigationControllerWithSbName:@"Discover"];
    
    UINavigationController *historyNav = [self navigationControllerWithSbName:@"History"];
    
    UINavigationController *myLotteryNav = [self navigationControllerWithSbName:@"MyLottery"];
    
    self.viewControllers = @[hallNav,arenaNav,discoverNav,historyNav,myLotteryNav];

}

/// 通过故事板名称加载子控制器
- (UINavigationController *)navigationControllerWithSbName:(NSString *)sbName {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
    
    UINavigationController *nav = [sb instantiateInitialViewController];
    
    //导航控制器里面有栈区，栈区里面装着所有的子控制器topViewController
    nav.topViewController.view.backgroundColor = ZZRandomColor;
    
    return nav;

}



@end
